package com.fastadmin.mapper.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fastadmin.pojo.XuQiu;
import com.fastadmin.pojo.FastParam;
import com.google.common.collect.Lists;
import com.xphsc.easyjdbc.EasyJdbcTemplate;
import com.xphsc.easyjdbc.page.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.xphsc.easyjdbc.core.metadata.ElementResolver;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Repository
@Slf4j
public class XuQiuMapper extends BaseMapper {
    @Autowired
    EasyJdbcTemplate easyJdbcTemplate;

    public PageInfo<XuQiu> findByFastParam(FastParam fastParam) {
        String tableName = ElementResolver.resolve(XuQiu.class).getTable();
        PageInfo<XuQiu> pageInfo = this.easyJdbcTemplate.findByPage(fastParam.getSql(tableName), XuQiu.class, fastParam.getOffset(), fastParam.getLimit());
        return pageInfo;
    }

    public XuQiu findById(Long ids) {
        XuQiu xuqiu = this.easyJdbcTemplate.getByPrimaryKey(XuQiu.class, ids);
        return xuqiu;
    }

    public void deleteById(String id) {
        this.easyJdbcTemplate.deleteByPrimaryKey(XuQiu.class, id);
    }

    public void update(XuQiu xuqiu) {
        xuqiu.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        this.easyJdbcTemplate.update(xuqiu);
    }

    public Object save(XuQiu xuqiu) {
        xuqiu.setCreate_time(new Timestamp(System.currentTimeMillis()));
        xuqiu.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        return this.easyJdbcTemplate.insertKey(xuqiu);

    }

    public List<XuQiu> findByIds(String ids) {
        if (StrUtil.isBlank(ids)) {
            return CollUtil.newArrayList();
        }
        return this.easyJdbcTemplate.findByIds(XuQiu.class, Arrays.asList(StrUtil.splitToArray(ids, ',')));

    }

    public PageInfo<XuQiu> findByOpenId(String open_id) {
        String sql = "SELECT * FROM t_xuqiu WHERE openid='" + open_id + "' ORDER BY id DESC";
        PageInfo<XuQiu> page = this.easyJdbcTemplate.findByPage(sql, XuQiu.class, 0, 10);
        return page;

    }

    public PageInfo<XuQiu> findByType(Integer endPos, Integer step, Integer t) {
        ArrayList<Object> params = Lists.newArrayList();

        String sql = "SELECT * FROM t_xuqiu WHERE 1=1";
        if (ObjectUtil.isNotNull(t)) {
            if (t == 1) {
                sql += " AND t= ?";

                params.add(t);
            }

        }
        PageInfo<XuQiu> pageInfo = easyJdbcTemplate.findByPage(sql, XuQiu.class, step, endPos, params.toArray());
        return pageInfo;
    }
}
